import React from 'react';
import Layout from './layout';
import { Provider } from 'mobx-react';
import store from './stores';
import Utils from 'common/utils';
import keycode from 'keycode';
import HOT_KEY from 'common/const/hotKey';

const { addEvent, removeEvent } = Utils;
// 热键
const onKeyDown = (e) => {
  const key = keycode(e);
  // console.log('按下', e, key);
  if (HOT_KEY.indexOf(key) !== -1) {
    CFG.hotKey[key] = true;
  }
};
const onKeyUp = (e) => {
  const key = keycode(e);
  // console.log('释放', e, key);
  if (HOT_KEY.indexOf(key) !== -1) {
    CFG.hotKey[key] = false;
  }
};

export default class Editor extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    addEvent(document, 'keydown', onKeyDown);
    addEvent(document, 'keyup', onKeyUp);
  }

  componentWillUnmount() {
    removeEvent(document, 'keydown', onKeyDown);
    removeEvent(document, 'keyup', onKeyUp);
  }

  render() {
    return (
      <Provider store={store} >
        <Layout />
      </Provider>
    );
  }
};
